from datetime import datetime

from api.login_api import loginApi
from utils.RequestUtils import Request


class SetmealOrderApi:

    def __init__(self):
        self.request = Request()

    def store(self):
        """
        获取所有的商店接口
        :return:
        """
        url = "https://stage.geer2.com/ym/goodsStore/order/storePage"
        params = {
            "current": 1,
            "size": 20,
            "managementType": 0
        }
        headers = {
            "Authorization": "Bearer" + " " + loginApi().login()
        }
        response = self.request.get(url=url, params=params, headers=headers)

        return response

    def userID(self, storeId=None):
        """
        获取所有商店中所有的会员的信息
        :param storeId:
        :return:
        """
        url = f'https://stage.geer2.com/ym/member/order/customerPage?current=1&size=20&storeId={storeId}&keyWord='
        headers = {
            "Authorization": "Bearer" + " " + loginApi().login()
        }
        response = self.request.get(url=url, headers=headers)
        return response

    def order_setmeal(self, storeId, customerId):
        """
        获取套餐信息
        :return:
        """
        url = f"https://stage.geer2.com/ym/suit/pageSuit?current=1&size=20&storeId={storeId}&customerId={customerId}"

        headers = {
            "Authorization": "Bearer" + " " + loginApi().login()
        }
        response = self.request.get(url=url, headers=headers)
        return response

    def detailById(self, id=2696):
        """
        获取单个会员的信息
        :param storeId:
        :return:
        """
        url = f'https://stage.geer2.com/sys/customer/wallet/detailById/{id}'
        headers = {
            "Authorization": "Bearer" + " " + loginApi().login()
        }
        response = self.request.get(url=url, headers=headers)
        return response

    def order_create(self, storeId, userId, Price, goodsId, goodsId2, suitId, ruleId1, ruleId2):
        """
        创建服务订单接口
        :param storeId: 商店编号
        :param userId: 用户编号
        :param Price: 价格
        :param goodsId: 商品编号
        :return: 返回响应结果
        """
        url = "https://stage.geer2.com/ym/order/offline/order/create"
        create_time = str(datetime.now())
        data = {"createType": "suit", "isExamineAgree": False, "rollbackFlag": 1, "counselor": "", "revenueFlag": 1,
                "couponCode": "",
                "createOrderBySuitParams":
                    {"storeId": storeId, "customerId": userId, "customPayPrice": Price, "sellers": [],
                     "payMode": "batefly", "createTime": create_time.split('.')[0],
                     "remark": "天天向上", "isOpenRevenueStatistics": 1, "isSupplementAmount": 1,
                     "paramsList": [
                         {
                             "goodsId": suitId,
                             "goodsType": "suit",
                             "storeId": storeId,
                             "goodsCount": 1,
                             "suitSelectList": [{"goodsId": goodsId, "suitRuleId": ruleId1},
                                                {"goodsId": goodsId2, "suitRuleId": ruleId2
                                                 }]}]}}
        headers = {
            "Authorization": "Bearer" + " " + loginApi().login()
        }

        response = self.request.post(url=url, json=data, headers=headers)
        return response

    def all_order_search(self, orderCode=None, pageSize=10, pageNum=1, companyId=319, companyIdName="柒美财润店"):
        """
        条件查询全部订单
        :param orderCode: 订单搜索
        :param pageSize: 页数长度：默认最短10，20,30,40,50,100
        :param pageNum:默认页数从第一页开始，1
        :param companyId: 门店ID
        :param companyIdName: 门店名称
        :return: 返回响应结果
        """
        url = "https://stage.geer2.com/ym/order/page"
        params = {
            "orderType": "all",
            "pageNum": pageNum,
            "pageSize": pageSize,
            "isRefundIgnore": 1,
            "companyId": companyId,
            "orderCode": orderCode,
            "$companyId": companyIdName
        }
        headers = {
            "Authorization": "Bearer" + " " + loginApi().login()
        }
        response = self.request.get(url=url, params=params, headers=headers)
        return response

    def order_editor(self, id):
        """
        编辑服务订单
        :param id:
        :return:
        """
        url = f'https://stage.geer2.com/ym/order/orderEditView/{id}'
        headers = {
            "Authorization": "Bearer" + " " + loginApi().login()
        }
        response = self.request.get(url=url, headers=headers)
        return response

    def order_update(self,orderId):
        """
        编辑套餐订单
        :param orderId: 套餐订单ID
        :return:
        """
        url = 'https://stage.geer2.com/ym//order/offline/order/edit'
        headers = {
            "Authorization": "Bearer" + " " + loginApi().login()
        }
        data = {"createType": "suit", "isExamineAgree": False, "orderId": orderId, "rollbackFlag": 1, "counselor": "",
                "revenueFlag": 1, "couponCode": "",
                "createOrderBySuitParams": {"storeId": 319, "customerId": 2696, "customPayPrice": 111, "sellers": [],
                                            "payMode": "batefly", "createTime": "2024-05-21 17:56:47",
                                            "remark": "天天向上,好好学习", "isOpenRevenueStatistics": 1, "isSupplementAmount": 1,
                                            "walletPayAmount": 0, "originCustomerId": 2696, "paramsList": [
                        {"goodsId": "3", "goodsType": "suit", "storeId": 319, "goodsCount": 1,
                         "suitSelectList": [{"goodsId": 279, "suitRuleId": 9}, {"goodsId": 256, "suitRuleId": 9}]}]}}
        response = self.request.post(url=url,json=data,headers=headers)
        return response

    def order_details(self, id):
        """
        订单详情接口
        :param id: 订单ID
        :return: 返回响应结果
        """
        url = f"https://stage.geer2.com/ym/order/{id}"
        headers = {
            "Authorization": "Bearer" + " " + loginApi().login()
        }
        response = self.request.get(url=url, headers=headers)
        return response

    def order_pass(self, id):
        """
        服务订单审核通过
        :param id:
        :return:
        """
        url = f"https://stage.geer2.com/ym/order/examineAgree/{id}"
        headers = {
            "Authorization": "Bearer" + " " + loginApi().login()
        }
        response = self.request.get(url=url, headers=headers)
        return response

    def order_applyRefund(self, text, orderId):
        """
        提出申请退款接口
        :param text:
        :param orderId:
        :return:
        """
        url = "https://stage.geer2.com/ym/order/applyRefund"
        data = {"customerRemark": text, "orderId": orderId,
                "refundStatus": {"orderId": orderId, "refundStatus": "applying"}}
        headers = {
            "Authorization": "Bearer" + " " + loginApi().login()
        }
        response = self.request.post(url=url, json=data, headers=headers)
        return response

    def order_agree(self, orderId):
        """
        同意申请退款接口
        :param orderId:
        :return:
        """
        url = "https://stage.geer2.com/ym/order/verifyOrderDelete/{}".format(orderId)

        headers = {
            "Authorization": "Bearer" + " " + loginApi().login()
        }
        response = self.request.get(url=url, headers=headers)
        return response

    def order_refund(self, text, orderid):
        """
        拒绝退款
        :param orderid:
        :param text:
        :return:
        """
        url = "https://stage.geer2.com/ym/order/refund"
        data = {"refundReason": text, "orderId": orderid, "refundStatus": "applying"}
        headers = {
            "Authorization": "Bearer" + " " + loginApi().login()
        }
        response = self.request.post(url=url, headers=headers)
        return response

    def order_orderDelete(self, orderId):
        """
        删除服务订单
        :param orderId:
        :return:
        """
        url = "https://stage.geer2.com/ym/order/orderDelete/{}".format(orderId)

        headers = {
            "Authorization": "Bearer" + " " + loginApi().login()
        }
        response = self.request.get(url=url, headers=headers)
        return response

    def order_examineRefuse(self, text: str, orderId):
        """
        驳回服务订单
        :param orderId:
        :return:
        """
        url = "https://stage.geer2.com/ym/order/examineRefuse"
        data = {"orderId": orderId, "refuseReason": text}
        headers = {
            "Authorization": "Bearer" + " " + loginApi().login()
        }
        response = self.request.post(url=url, json=data, headers=headers)
        return response



if __name__ == '__main__':
    # SetmealOrderApi().userID(319)
    # SetmealOrderApi().detailById()

    # storeId, userId, Price, goodsId, suitId, ruleId

    # storeId = SetmealOrderApi().store().json()['data']["records"][0].get('storeId')
    # userId = SetmealOrderApi().userID(storeId).json()['data']["records"][0].get('id')
    # Price1 = int(SetmealOrderApi().order_setmeal(
    #     storeId, userId).json()['data']['records'][0]['salePrice'])
    #
    # goodsId1 = SetmealOrderApi().order_setmeal(
    #     storeId, userId).json()['data']['records'][0]['suitRuleList'][0]['suitRuleItemList'][0]['itemId']
    # goodsId2 = SetmealOrderApi().order_setmeal(
    #     storeId, userId).json()['data']['records'][0]['suitRuleList'][0]['suitRuleItemList'][1]['itemId']
    # suitId = SetmealOrderApi().order_setmeal(
    #     storeId, userId).json()['data']['records'][0]['id']
    # ruleId1 = SetmealOrderApi().order_setmeal(
    #     storeId, userId).json()['data']['records'][0]['suitRuleList'][0]['suitRuleItemList'][0]['ruleId']
    # ruleId2 = SetmealOrderApi().order_setmeal(
    #     storeId, userId).json()['data']['records'][0]['suitRuleList'][0]['suitRuleItemList'][0]['ruleId']

    # SetmealOrderApi().order_create(storeId=storeId, userId=userId, Price=Price1,
    #                                goodsId=goodsId1, goodsId2=goodsId2, suitId=suitId, ruleId1=ruleId1, ruleId2=ruleId2)

    SetmealOrderApi().order_update(20866)




